package ge.telasi.tasks.ui.comp;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;

/**
 * @author dimitri
 */
public abstract class ImagePanel extends JPanel {
    private JLabel label;
    private boolean changeable;

    protected abstract void save(byte[] data);

    public ImagePanel() {
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(100, 100));
        setBorder(BorderFactory.createEtchedBorder());
        label = new JLabel();
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setVerticalAlignment(JLabel.CENTER);
        add(label, BorderLayout.CENTER);
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() > 1) {
                    select();
                }
            }
        });
        displayDefault();
    }

    private void select() {
        if (!isChangeable()) return;
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new ImageFilter());
        int resp = chooser.showDialog(this, "აარჩიეთ");
        if (resp != JFileChooser.APPROVE_OPTION) return;
        File f = chooser.getSelectedFile();
        try {
            FileInputStream in = new FileInputStream(f);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buff = new byte[1024];
            int size = 0;
            while ((size = in.read(buff)) != -1) {
                out.write(buff, 0, size);
            }
            byte[] data = out.toByteArray();
            ImageIcon icon = new ImageIcon(data);
            display(icon);
            save(data);
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }

    protected void displayDefault() {
        try {
            ImageIcon icon = new ImageIcon(ImagePanel.class.getResource("d_silhouette.gif"));
            display(icon);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    protected void display(ImageIcon icon) {
        int newWidth = 100;
        int newHeight = -1;
        label.setIcon(new ImageIcon(icon.getImage().getScaledInstance(newWidth, newHeight, 0)));
    }

    private class ImageFilter extends FileFilter {
        private String getExtension(String fileName) {
            int index = fileName.lastIndexOf(".");
            String ext = null;
            if (index != -1) {
                ext = fileName.substring(index + 1).toUpperCase();
            }
            return ext;
        }

        @Override
        public boolean accept(File f) {
            if (f.isDirectory()) return true;
            String ext = getExtension(f.getName());
            return "PNG".equals(ext) || "GIF".equals(ext) ||
                   "JPG".equals(ext) ||
                   "JPEG".equals(ext);
        }

        @Override
        public String getDescription() {
            return "გამოსახულების ფაილები";
        }
    }

    public boolean isChangeable() {
        return changeable;
    }

    public void setChangeable(boolean changeable) {
        this.changeable = changeable;
    }

}
